import {BrowserWindow, ipcMain} from 'electron'
import WinURL from "./WinURL.class";

const options = {
    // 是否有边框
    frame: true,
    // autoHideMenuBar Boolean (可选) - 自动隐藏菜单栏, 除非按了Alt键. 默认值为false.
    autoHideMenuBar: true,
    // resizable Boolean (可选) - 窗口是否可以改变尺寸. 默认值为true.
    resizable: true,
    // useContentSize Boolean (可选) - width 和 height 将设置为 web 页面的尺寸(译注: 不包含边框), 这意味着窗口的实际尺寸将包括窗口边框的大小，稍微会大一点。 默认值为 false.
    useContentSize: true,
    // 背景色
    backgroundColor: '#cccccc',
    // 是否直接显示
    show: false,
    // 网页功能的设置
    webPreferences: {
        // 跨域设置
        webSecurity: false,
        // 使用的全局session
        // partition String (optional) - 通过 session 的 partition 字符串来设置界面session.
        // 如果 partition 以 persist:开头, 该页面将使用持续的 session，并在所有页面生效，且使用同一个partition.
        // 如果没有 persist: 前缀, 页面将使用 in-memory session. 通过分配相同的 partition, 多个页可以共享同一会话。 默认使用默认的 session.
        partition: "persist:login"
    }
}

class Win {
    constructor() {
        this.mainWindow = null;
        this.articleWindow = null;
        this.messageWindow = null;
        this.commonWindow = null;
        this.chatWindow = null;
    }

    /**
     * 主窗口
     * @returns {mainWindow}
     */
    static createMainWindow(url) {
        if (this.mainWindow) {
            this.mainWindow.loadURL(url)
            return this.mainWindow;
        }
        this.mainWindow = new BrowserWindow({
            frame: false,
            autoHideMenuBar: true,
            resizable: true,
            useContentSize: true,
            backgroundColor: '#cccccc',
            show: false,
            webPreferences: {
                webSecurity: false,
                partition: "persist:login"
            }
        })
        this.mainWindow.maximize()
        this.mainWindow.loadURL(url)
        this.mainWindow.once('ready-to-show', () => {
            this.mainWindow.show()
        })
        this.mainWindow.on('closed', () => {
            this.mainWindow = null;
        })
        //任务栏的进度条 0 - 1
        this.mainWindow.setProgressBar(0.5)
        return this.messageWindow;
    }

    /**
     * 创作窗口
     * @returns {articleWindow}
     */
    static createArticleWindow(url) {
        if (this.articleWindow) {
            this.articleWindow.loadURL(url)
            return this.articleWindow;
        }
        this.articleWindow = new BrowserWindow({
            frame: false,
            autoHideMenuBar: true,
            resizable: true,
            useContentSize: true,
            backgroundColor: '#cccccc',
            show: false,
            webPreferences: {
                webSecurity: false,
                partition: "persist:login"
            }
        })
        this.articleWindow.maximize()
        this.articleWindow.loadURL(url)
        this.articleWindow.once('ready-to-show', () => {
            this.articleWindow.show()
        })
        this.articleWindow.on('closed', () => {
            this.articleWindow = null;
        })
        return this.articleWindow;
    }

    /**
     * 消息窗口
     * @returns {messageWindow}
     */
    static createMessageWindow(url) {
        if (this.messageWindow) {
            this.messageWindow.loadURL(url)
            return this.messageWindow;
        }
        this.messageWindow = new BrowserWindow({
            frame: true,
            autoHideMenuBar: true,
            resizable: true,
            useContentSize: true,
            backgroundColor: '#cccccc',
            show: false,
            parent: this.mainWindow,
            webPreferences: {
                webSecurity: false,
                partition: "persist:login"
            }
        })
        this.messageWindow.loadURL(url)
        this.messageWindow.once('ready-to-show', () => {
            this.messageWindow.show()
        })
        this.messageWindow.on('closed', () => {
            this.messageWindow = null;
        })
        return this.messageWindow;
    }

    /**
     * 即时通讯窗口
     * @returns {chatWindow}
     */
    static createChatWindow(url) {
        if (this.chatWindow) {
            this.chatWindow.loadURL(url)
            return this.chatWindow;
        }
        this.chatWindow = new BrowserWindow({
            frame: false,
            autoHideMenuBar: true,
            resizable: true,
            useContentSize: true,
            backgroundColor: '#cccccc',
            show: false,
            webPreferences: {
                webSecurity: false,
                partition: "persist:login"
            }
        })
        this.chatWindow.loadURL(url)
        this.chatWindow.once('ready-to-show', () => {
            this.chatWindow.show()
        })
        this.chatWindow.on('closed', () => {
            this.chatWindow = null;
        })
        return this.chatWindow;
    }

    /**
     * 通用临时窗口
     * @returns {commonWindow}
     */
    static createCommonWindow(url) {
        if (this.commonWindow) {
            this.commonWindow.loadURL(url)
            return this.commonWindow;
        }
        this.commonWindow = new BrowserWindow({
            frame: true,
            autoHideMenuBar: true,
            resizable: true,
            useContentSize: true,
            backgroundColor: '#cccccc',
            show: false,
            webPreferences: {
                webSecurity: false,
                partition: "persist:login"
            }
        })
        this.commonWindow.loadURL(url)
        this.commonWindow.once('ready-to-show', () => {
            this.commonWindow.show()
        })
        this.commonWindow.on('closed', () => {
            this.commonWindow = null;
        })
        return this.commonWindow;
    }

}

ipcMain.on('to-createCommonWindow', (event, arg) => {
    console.log(arg)
    Win.createCommonWindow(arg);
})

ipcMain.on('min-window', (event, arg) => {
    BrowserWindow.fromId(arg).minimize()
})

ipcMain.on('max-window', (event, arg) => {
    if(BrowserWindow.fromId(arg).isMaximized()){
        BrowserWindow.fromId(arg).unmaximize()
    }else {
        BrowserWindow.fromId(arg).maximize()
    }
})

ipcMain.on('close-window', (event, arg) => {
    BrowserWindow.fromId(arg).close()
})

export default Win;
